﻿using System.Collections.Generic;
using UnityEngine;

namespace UFrame
{
    public class AnimatorEditorPlayer : TimeLineObjectBase<Animator>
    {
        bool recondFirish = false;
        float clipMaxTime = 0f;

        public AnimatorEditorPlayer(Animator animator)
        {
            if (animator != null)
            {
                obj = animator;
            }
        }

        public override string GetName()
        {
            return obj.name;
        }

        public List<string> GetSkillAnimationNames()
        {
            if (obj != null)
            {
                List<string> list = new List<string>();
                RuntimeAnimatorController runanimator = obj.runtimeAnimatorController;
                foreach (var v in runanimator.animationClips)
                {
                    if (v.name.ToLower().Contains("skill"))
                    {
                        list.Add(v.name.ToLower());
                    }
                }
                return list;
            }
            return null;
        }


        public void SetRecondAnimationClip(string name)
        {
            if (obj != null)
            {
                RuntimeAnimatorController runanimator = obj.runtimeAnimatorController;
                AnimationClip clip = null;
                foreach (var v in runanimator.animationClips)
                {
                    if (v.name.ToLower() == name)
                    {
                        clip = v;
                    }
                }
                if (clip == null)
                {
                    return;
                }

                int index = 0;
                if (name.Contains("skill"))
                {
                    string[] namess = name.Split('_');

                    int skilltype = int.Parse(namess[namess.Length - 2]);
                    int skillIndex = int.Parse(namess[namess.Length - 1]);
                    index = skilltype * 10 + skillIndex;
                }

                obj.StopPlayback();
                float frameTime = 1.0f / 30;
                int frame = (int)(clip.length * clip.frameRate);
                obj.StartRecording(frame);
                obj.recorderStartTime = 0f;
                obj.SetInteger("state", (int)AnimatorStateEnum.AnimatorSKILL);
                if (index != -1)
                {
                    obj.SetInteger("skilltype", index);
                }

                for (var j = 0; j <= frame; j++)
                {
                    obj.Update(frameTime);
                }

                obj.StopRecording();
                obj.StartPlayback();
                clipMaxTime = obj.recorderStopTime;
                recondFirish = true;
                TimeChange(0);
            }
        }

        public override void TimeChange(float time)
        {
            obj.playbackTime = time;
            obj.Update(0);
        }

        public float GetLength()
        {
            return clipMaxTime;
        }
    }
}
